package com.cupk.rotaryguard.monitor.job;

import com.cupk.common.base.config.RedisKeyContant;
import com.cupk.rotaryguard.monitor.entity.DeviceInfo;
import com.cupk.rotaryguard.monitor.entity.DeviceMonitorRecord;
import com.cupk.rotaryguard.monitor.mapper.DeviceMapper;
import com.cupk.rotaryguard.monitor.mapper.MonitorRecordMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import smile.data.type.DataType;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 模拟生成设备数据
 * 定时任务 xxl-job
 *
 * @author tjy
 * @date 2023/3/14
 */
@Component
public class DeviceDataGeneratorJob {

    @Resource
    private MonitorRecordMapper monitorRecordMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private Random random = new Random();

    public DeviceDataGeneratorJob() {
    }

    /**
     * 生成设备参数
     */
    @XxlJob("generatingData")
    public void startGeneratingData() {
        Long deviceId = Long.valueOf(XxlJobHelper.getJobParam());
        //根据任务传参获取当前设备id
        DeviceInfo curDevice = deviceMapper.selectById(deviceId);
        if (ObjectUtils.isEmpty(curDevice)) {
            return;
        }
        int maxRpm = 7200;
        int minPrm = 7000;
        double randomNumber = Math.random();
        // 格式化为两位小数
        DecimalFormat df = new DecimalFormat("#.00");
        // 生成DeTime
        double deTime = generateVibrationData();
        // 生成FeTime
        double feTime = generateVibrationData();
        //生成湿度
        double Humidity = random.nextInt(100 - 0 + 1) + 0;
        //生成ph值
        double phValue = Double.parseDouble(df.format(randomNumber * (11 - 6) + 6));
        //生成转速
        int rpm = random.nextInt(maxRpm - minPrm + 1) + minPrm;
        //生成温度
        double Temperature = Double.parseDouble(df.format(randomNumber * (70 - 20) + 20));

        DeviceMonitorRecord record = new DeviceMonitorRecord();
        record.setDeviceId(curDevice.getId());
        record.setDeviceModel(curDevice.getDeviceModel());
        record.setHumidity(BigDecimal.valueOf(Humidity));
        record.setPhValue(BigDecimal.valueOf(phValue));
        record.setTemperature(BigDecimal.valueOf(Temperature));
        record.setRpm(rpm);
        record.setDeTime(BigDecimal.valueOf(deTime));
        record.setFeTime(BigDecimal.valueOf(feTime));
        record.setRecordTime(LocalDateTime.now());
        curDevice.setStatus(1);
        //更新设备状态
        deviceMapper.updateById(curDevice);
        //更新redis
        Set keys = stringRedisTemplate.keys(RedisKeyContant.DEVICE_INFO + "*");
        stringRedisTemplate.delete(keys);
        //插入监控记录
        monitorRecordMapper.insert(record);
    }

    /**
     * 生成正常设备参数
     */
    @XxlJob("generatingNormalData")
    public void startGeneratingNormalData() {
        Long deviceId = Long.valueOf(XxlJobHelper.getJobParam());
        //根据任务传参获取当前设备id
        DeviceInfo curDevice = deviceMapper.selectById(deviceId);
        if (ObjectUtils.isEmpty(curDevice)) {
            return;
        }
        int maxRpm = 7200;
        int minPrm = 7000;
        double randomNumber = Math.random();
        // 格式化为两位小数
        DecimalFormat df = new DecimalFormat("#.00");
        // 生成DeTime
        double deTime = generateVibrationData();
        // 生成FeTime
        double feTime = generateVibrationData();
        //生成湿度
        double Humidity = random.nextInt(100 - 0 + 1) + 0;
        //生成ph值
        double phValue = Double.parseDouble(df.format(randomNumber * (11 - 6) + 6));
        //生成转速
        int rpm = random.nextInt(maxRpm - minPrm + 1) + minPrm;
        //生成温度
        double Temperature = Double.parseDouble(df.format(randomNumber * (70 - 20) + 20));

        DeviceMonitorRecord record = new DeviceMonitorRecord();
        record.setDeviceId(curDevice.getId());
        record.setDeviceModel(curDevice.getDeviceModel());
        record.setHumidity(BigDecimal.valueOf(Humidity));
        record.setPhValue(BigDecimal.valueOf(phValue));
        record.setTemperature(BigDecimal.valueOf(Temperature));
        record.setRpm(rpm);
        record.setDeTime(BigDecimal.valueOf(-0.07000954091816368));
        record.setFeTime(BigDecimal.valueOf(0.030407272727272724));
        record.setRecordTime(LocalDateTime.now());
        curDevice.setStatus(1);
        //更新设备状态
        deviceMapper.updateById(curDevice);
        //更新redis
        Set keys = stringRedisTemplate.keys(RedisKeyContant.DEVICE_INFO + "*");
        stringRedisTemplate.delete(keys);
        //插入监控记录
        monitorRecordMapper.insert(record);
    }
    private double generateVibrationData() {
        //生成数据
        return random.nextGaussian() * 0.1;
    }

}
